package com.js.nickcar.base;

import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.js.nickcar.R;

import butterknife.ButterKnife;

/**
 * Created by chiclaim on 2016/07/09
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {

    protected TextView mToolbarTitle;
    protected Toolbar mToolbar;
    protected View statubarView;

    protected Bundle mBundle;
    protected View mRoot;
    protected P mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        mPresenter = createPresenter();
        mPresenter.attachView(this);
        super.onCreate(savedInstanceState);
        mBundle = getArguments();
        initBundle(mBundle);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        if (mRoot != null) {
            ViewGroup parent = (ViewGroup) mRoot.getParent();
            if (parent != null)
                parent.removeView(mRoot);
        } else {//初次创建
            mRoot = inflater.inflate(getLayoutId(), container, false);
            ButterKnife.bind(this, mRoot);
            initWidght(mRoot, inflater);
            initData();//此处非首次请求数据
            setListener();
        }
        return mRoot;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) && statubarView != null) {
            ViewGroup.LayoutParams layoutParams = statubarView.getLayoutParams();
            layoutParams.height = getStatusBarHeight();
            statubarView.setLayoutParams(layoutParams);
        }
    }

    protected void initWidght(View mRoot, LayoutInflater inflater) {
        setToolBar();
    }

    public abstract int getLayoutId();

    protected abstract P createPresenter();

    protected abstract void initData();

    protected void initBundle(Bundle bundle) {
    }

    protected void setListener() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRoot = null;
        mBundle = null;
        ButterKnife.unbind(this);
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    public final void addFragment(Fragment fragment) {
        String tag = getActivity().getClass().toString();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .addToBackStack(tag)
                .replace(R.id.fl_container, fragment, tag)
                .commit();
    }

    public final void replaceFragment(Fragment fragment) {
        String tag = getActivity().getClass().toString();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fl_container, fragment, tag)
                .commit();
    }

    protected void setToolBar() {
        mToolbar =  findView(R.id.toolbar);
        mToolbarTitle = findView(R.id.toolbar_title);
        statubarView = findView(R.id.view_statubar);
        if (mToolbar != null) {
            String title = getTitle();
            mToolbar.setTitle("");
            mToolbar.setNavigationIcon(R.mipmap.ic_back);
            if (!TextUtils.isEmpty(title)) {
                mToolbarTitle.setText(title);
            }
            AppCompatActivity activity = (AppCompatActivity) getActivity();
            activity.setSupportActionBar(mToolbar);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            activity.getSupportActionBar().setDisplayHomeAsUpEnabled(isShowBack());
        }
    }

    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    protected String getTitle() {
        return "";
    }

    protected boolean isShowBack() {
        return true;
    }

    public Drawable getTintedDrawable(@NonNull Drawable inputDrawable, @ColorInt int color) {
        Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable);
        DrawableCompat.setTint(wrapDrawable, color);
        DrawableCompat.setTintMode(wrapDrawable, PorterDuff.Mode.SRC_IN);
        return wrapDrawable;
    }

    protected void finish() {
        getFragmentManager().popBackStack();
    }

    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg))
            Toast.makeText(getContext().getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
    /**
     * 找控件的方法
     */
    protected <T extends View> T findView(int viewId) {
        return (T) mRoot.findViewById(viewId);
    }
}